//
//  MNMSGSCViewController.m
//  HNJYB
//
//  Created by Mag1cPanda on 2017/3/23.
//  Copyright © 2017年 Mag1cPanda. All rights reserved.
//

#import "MNMSGSCViewController.h"
#import "MoreSGSCGroup.h"
#import "NMSGSCGroup.h"
#import "IDyz.h"
#import "MNMZRRDViewController.h"

@interface MNMSGSCViewController ()
< UITextFieldDelegate >
{
    UIScrollView *scroll;
    
    MoreSGSCGroup *mGroup;
    NMSGSCGroup *nmGroup;
    
    //机动车方
    NSString *mcarType;
    NSString *mname;
    NSString *mCarNo;
    NSString *mphoneNum;
    NSString *mdriveNum;
    NSString *mVinNo;
    NSString *mbdhNum;
    
    //非机动车方
    NSString *nmbrand;
    NSString *nmcartype;
    NSString *nmname;
    NSString *nmphoneno;
    NSString *nmidno;
}
@end

@implementation MNMSGSCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"事故上传";
    
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 64)];
    [self.view addSubview:scroll];
    
    mGroup = [[MoreSGSCGroup alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 570) groupId:@"mGroup"];
    mGroup.title = @"机动车方信息";
    mGroup.cphView.field.delegate = self;
    mGroup.jszView.field.delegate = self;
    mGroup.cjhView.field.delegate = self;
    mGroup.lxdhView.field.keyboardType = UIKeyboardTypePhonePad;
    //给获取按钮添加点击事件
    [mGroup.codeBtn addTarget:self action:@selector(getBdhCode:) forControlEvents:1 << 6];
    [scroll addSubview:mGroup];
    
    nmGroup = [[NMSGSCGroup alloc] initWithFrame:CGRectMake(0, mGroup.maxY, ScreenWidth, 440) groupId:@"nmGroup"];
    nmGroup.title = @"非机动车方信息";
    nmGroup.brandView.field.delegate = self;
    nmGroup.cartypeView.field.delegate = self;
    nmGroup.nameView.field.delegate = self;
    nmGroup.phoneView.field.keyboardType = UIKeyboardTypePhonePad;
    nmGroup.idcardView.field.delegate = self;
    [scroll addSubview:nmGroup];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(10, nmGroup.maxY + 25, ScreenWidth - 20, 50);
    [btn setTitle:@"提交" forState:0];
    [btn setTitleColor:[UIColor whiteColor] forState:0];
    [btn addTarget:self action:@selector(confirmBtnClicked) forControlEvents:1 << 6];
    btn.backgroundColor = HNBlue;
    btn.layer.cornerRadius = 5;
    [scroll addSubview:btn];
    
    scroll.contentSize = CGSizeMake(ScreenWidth, btn.maxY + 20);
    
    [self ocrAutofill];
}

#pragma mark - 获取保单号
- (void)getBdhCode:(UIButton *)btn
{
    //取值
    mCarNo = [NSString stringWithFormat:@"%@%@", mGroup.selectList.contentLab.text, mGroup.cphView.field.text];
    mVinNo = mGroup.cjhView.field.text;
 
    if (!([Util cheackLicense:mCarNo] || [Util checkLearnLicense:mCarNo]))
    {
        SHOWALERT(@"请填写正确的机动车方车牌号");
        return;
    }
    
    if (mGroup.isLinPai || ![mCarNo hasPrefix:@"豫"]) {
        if (![Util checkChaimsNO:mVinNo])
        {
            SHOWALERT(@"请填写正确的机动车方车架号");
            return;
        }
    }

    NSMutableDictionary *bdhBean = [NSMutableDictionary dictionary];
    
    [bdhBean setValue:GlobleInstance.userflag forKey:@"userflag"];
    [bdhBean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    [bdhBean setValue:GlobleInstance.token forKey:@"token"];
    [bdhBean setValue:mCarNo forKey:@"casecarno"];
    [bdhBean setValue:[mVinNo uppercaseString] forKey:@"frameno"];
    [bdhBean setValue:@"" forKey:@"policyno"];

    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:true];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappkckpSearchCpsNum" parameters:bdhBean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:true];
        
        //      NSLog(@"获取保单号 -> %@", result);
        
        if ([result[@"restate"] isEqualToString:@"1"])
        {
            mGroup.bdhView.field.text = result[@"data"][@"trafficinsno"];
        }
        
        else
        {
            MBProgressHUD *tHud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
            tHud.mode = MBProgressHUDModeText;
            tHud.label.text = @"未查询到保单号，请手动输入";
            [tHud hideAnimated:NO afterDelay:1.5];
        }
        
    }];
}

- (void)ocrAutofill
{
    
    NSMutableDictionary *ocrBean = [NSMutableDictionary dictionary];
    [ocrBean setValue:GlobleInstance.userflag forKey:@"userflag"];
    [ocrBean setValue:GlobleInstance.token forKey:@"token"];
    [ocrBean setValue:GlobleInstance.appcaseno forKey:@"appcaseno"];
    
    MBProgressHUD *ocrHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappgetOCRdata" parameters:ocrBean complete:^(id result, ResultType resultType) {
        
        [ocrHud hideAnimated:YES];
        NSLog(@"jjappgetOCRdata ~ %@",result);
        NSArray *dataArr = result[@"data"];
        
        for (NSDictionary *dic in dataArr) {
            
            //机动车方驾驶证
            if ([dic[@"imagetype"] isEqual:@"16"])
            {
                GlobleInstance.mocrname = dic[@"ocrname"];
                GlobleInstance.mocrcardno = dic[@"ocrcardno"];
            }
            //机动车方行驶证
            if ([dic[@"imagetype"] isEqual:@"77"])
            {
                GlobleInstance.mocrcarno = dic[@"ocrcarno"];
                GlobleInstance.mocrvin = dic[@"ocrvin"];
                GlobleInstance.mocrcartype = dic[@"ocrcartype"];
            }
            //非机动车方身份证正面
            if ([dic[@"imagetype"] isEqual:@"18"]) {
                GlobleInstance.nmyfocrname = dic[@"ocrname"];
                GlobleInstance.nmyfocrcardno = dic[@"ocrcardno"];
            }
            //非机动车方行驶证反面无需识别
            if ([dic[@"imagetype"] isEqual:@"19"]) {
                
            }
            
        }
        
        /**********************************************/
        
        //填充甲方
        if ([GlobleInstance.mocrcartype hasPrefix:@"小型"])
        {
            mGroup.radio1.checked = YES;
        }
        else
        {
            mGroup.radio2.checked = YES;
        }
        mGroup.xmView.field.text = GlobleInstance.jfocrname;
        if (GlobleInstance.jfocrcarno.length == 7)
        {
            NSString *jfRegion = [GlobleInstance.jfocrcarno substringWithRange:NSMakeRange(0, 1)];
            NSString *jfCarNumber = [GlobleInstance.jfocrcarno substringFromIndex:1];
            mGroup.selectList.contentLab.text = jfRegion;
            mGroup.cphView.field.text = jfCarNumber;
        }
        mGroup.jszView.field.text = GlobleInstance.jfocrcardno;
        mGroup.cjhView.field.text = GlobleInstance.jfocrvin;
        
        //填充乙方
        nmGroup.nameView.field.text = GlobleInstance.nmyfocrname;
        nmGroup.idcardView.field.text = GlobleInstance.nmyfocrcardno;
        
    }];

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    //机动车方车牌号
    if (textField == mGroup.cphView.field)
    {
        textField.text = [textField.text uppercaseString];
        
    }
    //机动车方车架号
    if (textField == mGroup.cjhView.field)
    {
        textField.text = [textField.text uppercaseString];
    }
    //机动车方驾驶证号
    if (textField == mGroup.jszView.field)
    {
        textField.text = [textField.text uppercaseString];
    }
    
    //非机动车方身份证号
    if (textField == nmGroup.idcardView.field)
    {
        textField.text = [textField.text uppercaseString];
    }
    
}

#pragma mark - 提交按钮点击事件
- (void)confirmBtnClicked
{
    
    //机动车方
    mcarType = mGroup.selectedIndex;
    mname = mGroup.xmView.field.text;
    mCarNo = [NSString stringWithFormat:@"%@%@", mGroup.selectList.contentLab.text, mGroup.cphView.field.text];;
    mphoneNum = mGroup.lxdhView.field.text;
    mdriveNum = mGroup.jszView.field.text;
    mVinNo = mGroup.cjhView.field.text;
    mbdhNum = mGroup.bdhView.field.text;
    
    //非机动车方
    nmbrand = nmGroup.brandView.field.text;
    nmcartype = nmGroup.selectedIndex;
    nmname = nmGroup.nameView.field.text;
    nmphoneno = nmGroup.phoneView.field.text;
    nmidno = nmGroup.idcardView.field.text;
    
    //机动车方Judge
    if (!mcarType)
    {
        SHOWALERT(@"请选择机动车方车型");
        return;
    }
    
    if (mname.length < 1)
    {
        SHOWALERT(@"请填写机动车方姓名");
        return;
    }
    
    if (!([Util cheackLicense:mCarNo] || [Util checkLearnLicense:mCarNo]))
    {
        SHOWALERT(@"请填写正确的机动车方车牌号");
        return;
    }
    
    if (![Util isMobileNumber:mphoneNum])
    {
        SHOWALERT(@"请填写正确的机动车方联系电话");
        return;
    }
    
    if (![IDyz validateIDCardNumber:mdriveNum])
    {
        SHOWALERT(@"请填写正确的机动车方驾驶证号");
        return;
    }
    
    if (mGroup.isLinPai || ![mCarNo hasPrefix:@"豫"]) {
        if (![Util checkChaimsNO:mVinNo])
        {
            SHOWALERT(@"请填写正确的机动车方车架号");
            return;
        }
    }
    
    //非机动车方Judge
    if (nmbrand.length < 1)
    {
        SHOWALERT(@"请填写非机动车方车辆品牌");
        return;
    }
    
    if (!nmcartype)
    {
        SHOWALERT(@"请选择非机动车方车型");
        return;
    }
    
    if (nmname.length < 1)
    {
        SHOWALERT(@"请填写非机动车方姓名");
        return;
    }
    
    if (![Util isMobileNumber:nmphoneno])
    {
        SHOWALERT(@"请填写正确的非机动车方联系电话");
        return;
    }
    
    if (![IDyz validateIDCardNumber:nmidno])
    {
        SHOWALERT(@"请填写正确的非机动车方身份证号");
        return;
    }
    
    
    
    if ([mphoneNum isEqualToString:nmphoneno])
    {
        SHOWALERT(@"双方手机号不能相同");
        return;
    }
    
    if ([mdriveNum isEqualToString:nmidno])
    {
        SHOWALERT(@"双方证件号不能相同");
        return;
    }

    
    //取值
    mCarNo = [NSString stringWithFormat:@"%@%@", mGroup.selectList.contentLab.text, mGroup.cphView.field.text];
    
    //临牌非河南
    if (mGroup.isLinPai && ![mCarNo hasPrefix:@"豫"]) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请人工核实外省临牌车辆的证件信息真伪" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"认可提交"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self uploadCaseInfo];
            
        }];
        [alertController addAction:okAction];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"人工核实"style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancelAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    //非临牌非河南
    else if (!mGroup.isLinPai && ![mCarNo hasPrefix:@"豫"])
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请人工核实外省车辆的证件信息真伪" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"认可提交"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self uploadCaseInfo];
            
        }];
        [alertController addAction:okAction];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"人工核实"style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancelAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    //临牌河南
    else if (mGroup.isLinPai && [mCarNo hasPrefix:@"豫"])
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请人工核实临牌车辆的证件信息真伪" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"认可提交"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self uploadCaseInfo];
            
        }];
        [alertController addAction:okAction];
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"人工核实"style:UIAlertActionStyleDefault handler:nil];
        [alertController addAction:cancelAction];
        
        [self presentViewController:alertController animated:YES completion:nil];
    }
    //河南非临牌
    else {
        [self uploadCaseInfo];
    }
    
}

-(void)uploadCaseInfo
{
    NSMutableDictionary *bean = [NSMutableDictionary dictionary];
    
    NSArray *arr = @[@{@"carownname":mname,
                       @"carownphone":mphoneNum,
                       @"casecarno":mCarNo,
                       @"cartype":mcarType,
                       @"frameno":mVinNo,
                       @"cardno":mdriveNum,
                       @"driverlicence":mdriveNum,
                       @"party":@"1",
                       @"temporary":[NSString stringWithFormat:@"%d",mGroup.isLinPai]},
                     @{@"carownname":nmname,
                       @"carownphone":nmphoneno,
                       @"casecarno":@"",
                       @"cartype":nmcartype,
                       @"frameno":@"",
                       @"cardno":nmidno,
                       @"driverlicence":@"",
                       @"party":@"0"}];
    
    NSString *appcaseno = GlobleInstance.appcaseno;
    
    [bean setValue:arr forKey:@"casecarlist"];
    [bean setValue:appcaseno forKey:@"appcaseno"];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [LSHttpManager requestUrl:HNServiceURL serviceName:@"jjappcheckcbxx" parameters:bean complete:^(id result, ResultType resultType) {
        
        [hud hideAnimated:YES];
        
        if (result) {
            NSLog(@"承保信息 ~ %@",[Util objectToJson:result]);
        }
        
        if ([result[@"restate"] isEqualToString:@"1"]) {
            MNMZRRDViewController *vc = [MNMZRRDViewController new];
            
            InfoModel *jfModel = [InfoModel new];
            jfModel.carType = mcarType;
            jfModel.name = mname;
            jfModel.carNum = mCarNo;
            jfModel.phoneNum = mphoneNum;
            jfModel.driveNum = mdriveNum;
            jfModel.vinNum = mVinNo;
            jfModel.bdhNum = mbdhNum;
            
            InfoModel *yfModel = [InfoModel new];
            yfModel.carType = nmcartype;
            yfModel.name = nmname;
            yfModel.carNum = nmbrand;//非机动车carno存车辆品牌
            yfModel.phoneNum = nmphoneno;
            yfModel.driveNum = nmidno;
            
            vc.jfModel = jfModel;
            vc.yfModel = yfModel;
            vc.selectedItem = _selectedItem;
            vc.caseDes = _caseDes;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        else {
            SHOWALERT(result[@"redes"]);
        }
        
    }];

}

@end
